package com.song.uaa.gateway.web.rest;

import java.security.Principal;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.song.uaa.gateway.service.UserService;
import com.song.uaa.gateway.service.dto.AdminUserDTO;

import reactor.core.publisher.Mono;

@RestController
@RequestMapping("/api/account")
public class AccountResource {
	
	private UserService userService;
	
	public AccountResource(UserService userService) {
		this.userService = userService;
	}
	
	@GetMapping
	public Mono<AdminUserDTO> getAccount(Principal principal) {
		if(principal instanceof AbstractAuthenticationToken) {
			return userService.getUserFromAuthentication((AbstractAuthenticationToken)principal);
		} else {
			throw new AccountResourceException("用户未找到");
		}
	}

	private static class AccountResourceException extends RuntimeException {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		private AccountResourceException(String message) {
			super(message);
		}
	}
}
